function formatDate(date, formatStr) {
  function $addZero(v, size) {
    for (var i = 0, len = size - (v + "").length; i < len; i++) {
      v = "0" + v;
    };
    return v + "";
  };
  date = new Date(date);
  //格式化时间
  var arrWeek = ['日', '一', '二', '三', '四', '五', '六'],
    str = formatStr
      .replace(/yyyy|YYYY/, date.getFullYear())
      .replace(/yy|YY/, $addZero(date.getFullYear() % 100, 2))
      .replace(/mm|MM/, $addZero(date.getMonth() + 1, 2))
      .replace(/m|M/g, date.getMonth() + 1)
      .replace(/dd|DD/, $addZero(date.getDate(), 2))
      .replace(/d|D/g, date.getDate())
      .replace(/hh|HH/, $addZero(date.getHours(), 2))
      .replace(/h|H/g, date.getHours())
      .replace(/ii|II/, $addZero(date.getMinutes(), 2))
      .replace(/i|I/g, date.getMinutes())
      .replace(/ss|SS/, $addZero(date.getSeconds(), 2))
      .replace(/s|S/g, date.getSeconds())
      .replace(/w/g, date.getDay())
      .replace(/W/g, arrWeek[date.getDay()]);
  return str;
}
function formatDate2(createTime){
  
    var time=new Date(createTime);
    time=time.getFullYear() + "." +
     ((time.getMonth() + 1)<10?'0'+(time.getMonth() + 1):(time.getMonth() + 1)) + "." + 
    ((time.getDate() + 1)<10?'0'+(time.getDate() + 1):(time.getDate() )) + " " +
     ((time.getHours() + 1)<10?'0'+(time.getHours() + 1):(time.getHours() )) + ":" +
      ((time.getMinutes() + 1)<10?'0'+(time.getMinutes() + 1):(time.getMinutes() ))  ;
    return time;


}
function formatDate3(createTime){
  var time=new Date(createTime);
    time=time.getFullYear() + "-" + 

    ((time.getMonth() + 1)<10?'0'+(time.getMonth() + 1):(time.getMonth() + 1))

    + "-" + (time.getDate()<10?'0'+time.getDate():time.getDate()) ;
    return time;
}
function formatDateMD(createTime){
  var time=new Date(createTime);
    time= (time.getMonth() + 1) + "月" + time.getDate() 
    return time;
}
function verifyPhone(phone){
  return /^1(3|4|5|7|8|9)\d{9}$/.test(phone)
}

function check(e,allPart){
  /**获取到当前选择的part，是阅读题还是文法题等等 */
  var partId=e.currentTarget.dataset.partid;
 /**获取到当前选择的是第几题 */
  var qnum=e.target.dataset.qnum;
  /**获取到当前选择的是abcd中的哪一个 */
  var anum=e.target.dataset.anum;
  /**提前创好一个对象，以便放进dataset中 */
  var param={};
  /**修改allpart中的哪个choose的哪个选项，并更改check的值 */
  var str=`${allPart}[${partId}].choose[${qnum}].check`;
  param[str]=anum;
  return param;
   
}
function addLikeContent(e){
  let contentId=app.E(e,"myid");
  let userLike=app.E(e,'islike');
  let index=app.E(e,'idx');
  console.log(contentId,userLike,index)
  var  url;
  if(userLike){
    url =`${apiHost.config.portalApiHost}/portal/content/removeLike`;
    let data={contentId}
    httpJson.post(url,data,res=>{
      if(res.data.errorCode==0){
        var params={};
        var str=`list[${index}].userLike`;
        params[str]=false;
       this.setData(params)
      }
    })
  }
  if(!userLike){
    url =`${apiHost.config.portalApiHost}/portal/content/addLike`;
    let data={contentId}
    httpJson.post(url,data,res=>{
      if(res.data.errorCode==0){
        var params={};
        var str=`list[${index}].userLike`;
        params[str]=true;
       this.setData(params)
      }
    })
  }
 


};
function successToast(msg,time){
  wx.showToast({
    title: msg,
    icon: 'success',
    duration: time
  })
  setTimeout(()=>{
      wx.hideToast()
    },time)
}
function errToast(msg,time){
  wx.showToast({
    title: msg,
    icon: 'none',
    duration: time
  })
  setTimeout(()=>{
      wx.hideToast()
    },time)

}





module.exports = {
  formatDate,
  verifyPhone,
  check,
  successToast,
  errToast,
  formatDate2,
  formatDate3,
  formatDateMD,
  
 
}